package com.zjitc.pigspring.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.zjitc.pigspring.bean.po.Order;
import com.zjitc.pigspring.bean.po.OrderVO;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface OrderMapper extends BaseMapper<Order> {

    @Select("select orders.id as id,orders.good_id as goodId,orders.address_id as addressId," +
            "goods.seller_id as sellerId,orders.user_id as userId,orders.count as count," +
            "orders.all_price as allPrice,orders.`status` as `status`,goods.img as goodImg," +
            "goods.`name` as goodName,goods.price as goodPrice,address.username as `user`," +
            "address.phone as phone,address.address as address,goods.unit as unit," +
            "seller.`name` as sellerName from orders left join goods on orders.good_id = goods.id " +
            "left join seller on goods.seller_id = seller.id left join address on orders.address_id = address.id " +
            "where orders.user_id = #{id}")
    List<OrderVO> getByUser(Integer id);

    @Delete("delete from orders where address_id=#{id}")
    boolean deleteByAddressId(Integer id);

    @Select("select orders.id as id,orders.good_id as goodId,orders.address_id as addressId," +
            "goods.seller_id as sellerId,orders.user_id as userId,orders.count as count," +
            "orders.all_price as allPrice,orders.`status` as `status`,goods.img as goodImg," +
            "goods.`name` as goodName,goods.price as goodPrice,address.username as `user`," +
            "address.phone as phone,address.address as address,goods.unit as unit," +
            "seller.`name` as sellerName from orders left join goods on orders.good_id = goods.id " +
            "left join seller on goods.seller_id = seller.id left join address on orders.address_id = address.id " +
            "where 1=1")
    List<OrderVO> getAll();

    @Select("select orders.id as id,orders.good_id as goodId,orders.address_id as addressId," +
            "goods.seller_id as sellerId,orders.user_id as userId,orders.count as count," +
            "orders.all_price as allPrice,orders.`status` as `status`,goods.img as goodImg," +
            "goods.`name` as goodName,goods.price as goodPrice,address.username as `user`," +
            "address.phone as phone,address.address as address,goods.unit as unit," +
            "seller.`name` as sellerName from orders left join goods on orders.good_id = goods.id " +
            "left join seller on goods.seller_id = seller.id left join address on orders.address_id = address.id " +
            "where goods.name LIKE CONCAT('%', #{goodName}, '%') ")
    List<OrderVO> getAllByGoodName(String goodName);

    @Delete("delete from orders where good_id = #{id}")
    boolean deleteByGoodId(Integer id);

    @Delete("delete from orders where user_id = #{id}")
    boolean deleteByUserId(Integer id);

    @Select("select orders.id as id,orders.good_id as goodId,orders.address_id as addressId," +
            "goods.seller_id as sellerId,orders.user_id as userId,orders.count as count," +
            "orders.all_price as allPrice,orders.`status` as `status`,goods.img as goodImg," +
            "goods.`name` as goodName,goods.price as goodPrice,address.username as `user`," +
            "address.phone as phone,address.address as address,goods.unit as unit," +
            "seller.`name` as sellerName from orders left join goods on orders.good_id = goods.id " +
            "left join seller on goods.seller_id = seller.id left join address on orders.address_id = address.id " +
            "where goods.seller_id = #{id}")
    List<OrderVO> selectBySeller(Integer id);

    @Select("select orders.id as id,orders.good_id as goodId,orders.address_id as addressId," +
            "goods.seller_id as sellerId,orders.user_id as userId,orders.count as count," +
            "orders.all_price as allPrice,orders.`status` as `status`,goods.img as goodImg," +
            "goods.`name` as goodName,goods.price as goodPrice,address.username as `user`," +
            "address.phone as phone,address.address as address,goods.unit as unit," +
            "seller.`name` as sellerName from orders left join goods on orders.good_id = goods.id " +
            "left join seller on goods.seller_id = seller.id left join address on orders.address_id = address.id " +
            "where goods.name LIKE CONCAT('%', #{goodName}, '%') and goods.seller_id = #{sellerId}")
    List<OrderVO> queryBySeller(OrderVO orderVO);
}
